<% manages_admins = manages_admins?
   can_add = @team.name == Team::ADMINS ? manages_admins : manages_team_members?(@team) %>
<%= section_panel 'Team Details', :width => 'double', :position => 'right' do %>
  <%= render('base/shared/edit_form',
             {:resource    => @team,
              :content     => 'team_details_content',
              :read_only   => !is_admin?,
              :cancel_url  => organization_teams_path,
              :form_url    => @team.new_record? ? organization_teams_path : organization_team_path(@team),
              :destroy_url => @team.new_record? || @team.name == Team::ADMINS ? nil : organization_team_path(@team)}) %>
<% end %>

<% unless @team.new_record? %>
	<%= section_panel icon('user', 'User Members'),
                    :width    => 'double',
                    :position => 'right',
                    :menu     => can_add ? [link_to_function(icon('plus', 'Add', 'fa-fw'), 'show_add_team_member_dialog("user")', :remote => true, :class => 'btn btn-mini')] : nil do %>
		<div id="team_user_members">
			<%= render('organization/team_members/user_list') %>
		</div>
	<% end %>
  <br>
  <%= section_panel icon('users', 'Group Members'),
                     :width    => 'double',
                     :position => 'right',
                     :menu     => can_add ? [link_to_function(icon('plus', 'Add', 'fa-fw'), 'show_add_team_member_dialog("group")', :remote => true, :class => 'btn btn-mini')] : nil do %>
		<div id="team_group_members">
			<%= render('organization/team_members/group_list') %>
		</div>
	<% end %>
  <% %w(user group).each do |type| %>
    <div id="add_team_<%= type %>" class="modal hide">
       <div class="modal-header">
         <button class="close" data-dismiss="modal">&times;</button>
         <h3>Add <%= type.capitalize %></h3>
       </div>
       <div class="modal-body" style="height:250px; overflow-x: hidden">
         <p>Team: <b><%= @team.name %></b></p>
         <%= form_tag(organization_team_members_path(@team), :remote => true, :class => 'form-inline') do %>
           <%= text_field_tag type, '', :class => 'input-xlarge', :required => true, :autocomplete => 'off', :placeholder => "Enter #{type} name..." %>
           <%= submit_tag "Add #{type}", :class => 'btn btn-success' %>
         <% end %>
         <div class="alert hide"></div>
       </div>
       <div class="modal-footer">
         <button class="btn" data-dismiss="modal">Close</button>
       </div>
   	</div>
  <% end %>
<% end %>
<script>
  window.show_add_team_member_dialog = function(type, message, error) {
    var dialog = $j(type === 'user' ? '#add_team_user' : "#add_team_group");
    dialog.find("input[type=text]").val('').focus();
    var alertEl = dialog.find(".alert");
    if (message) {
      alertEl.html(message).removeClass("alert-error").addClass('alert-success').show();
    }
    else if (error) {
      alertEl.html(error).removeClass("alert-success").addClass('alert-error').show();
    }
    else {
      alertEl.hide();
    }
    dialog.modal("show");
  };

  var userLookupFn = function (query, process) {
    return $j.get('<%= user_lookup_path %>',
        {login: query},
        function (data) {
          return process(data);
        });
  };

  var groupLookupFn = function (query, process) {
    return $j.get('<%= lookup_account_groups_path %>',
        {name: query},
        function (data) {
          return process(data);
        });
  };

  var updaterFn = function(item) {
    return item.split(' ')[0];
  };

  $j('#add_team_user input#user').typeahead({source: userLookupFn, updater: updaterFn});
  $j('#add_team_group input#group').typeahead({source: groupLookupFn, updater: updaterFn});
</script>
